package com.yonyou.pmclouds.personnel.process;

import com.yonyou.pmclouds.basecom.bp.processor.MultiProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.outter.tenantsdk.rmiitf.IUserInfoService;
import com.yonyou.pmclouds.personnel.entity.PersonnelVO;
import com.yonyou.pmclouds.user.entity.UserInfo;
import com.yonyou.pmclouds.user.entity.UserSingleQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author chulf
 * @Date 2020/4/15
 * @Description 进行用户的创建及反关联
 **/
@Component
@Slf4j
public class YhtUserRefBeforeMultiProcess implements MultiProcessor<PersonnelVO> {

    @Autowired
    private IUserInfoService iUserInfoService;

    @Override
    public void process(PersonnelVO[] vos, PersonnelVO[] origs) throws BusinessException {

        if (ArrayUtils.isEmpty(vos)) return;

        List<UserSingleQueryVO> result = iUserInfoService.isUsersExist(transUserInfo(vos));
        ArrayList<PersonnelVO> createUserList = new ArrayList<>();

        for (int i = 0; i < vos.length; i++) {
            if (result.get(i).getStatus() == 7) {
                 createUserList.add(vos[i]);
            } else {
                UserInfo user = result.get(i).getUser();

                if (user == null){
                    user = new UserInfo();
                    log.info(result.get(i).getMsg());
                }

                //由于返回的是密文 ，所以暂时不回写手机号及邮箱
               // if (StringUtils.isEmpty(vos[i].getPhone())) vos[i].setPhone(user.getUserMobile());
               // if (StringUtils.isEmpty(vos[i].getEmail())) vos[i].setEmail(user.getUserEmail());
                vos[i].setUserName(user.getUserName());
                vos[i].setUserId(user.getUserId());
            }
        }

        if (createUserList.isEmpty()) return;
        //创建友互通用户
        //TenantSdkUserInfo[] tenantSdkUserInfos = iUserInfoService.addUsers(PersonnelMaintainImpl.creatSdkUserByPersonel(createUserList.toArray(new PersonnelVO[0])));
        List<UserInfo> userInfos = iUserInfoService.batchCreateUsers(transUserInfo(createUserList.toArray(new PersonnelVO[0])));

        if (userInfos == null || userInfos.size() == 0) return;

        //这里维护userId给对应用户   这里的映射关系是通过名称，所以同一批次如果有相同名称的用户可能会有问题
        HashMap<String, PersonnelVO> keyToPersonnel = new HashMap<>();
        for (PersonnelVO vo : createUserList) {
            keyToPersonnel.put(vo.getSname(),vo);
        }
        for (UserInfo userInfo : userInfos) {
            PersonnelVO personnelVO = keyToPersonnel.get(userInfo.getUserName());

           // if (StringUtils.isEmpty(personnelVO.getEmail())) personnelVO.setEmail(userInfo.getUserEmail());
            personnelVO.setUserName(userInfo.getUserName());
            personnelVO.setUserId(userInfo.getUserId());

        }
    }
    private UserInfo[] transUserInfo(PersonnelVO[] vos){

        ArrayList<UserInfo> result = new ArrayList<>();

        for (PersonnelVO vo : vos) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserMobile(StringUtils.isEmpty(vo.getOriginalPhone()) ? null : vo.getOriginalPhone());
            userInfo.setUserEmail(StringUtils.isEmpty(vo.getOriginalEmail()) ? null : vo.getOriginalEmail());
            userInfo.setUserName(StringUtils.isEmpty(vo.getUserName()) ? vo.getSname() : vo.getUserName());
            result.add(userInfo);
        }
        return result.toArray(new UserInfo[0]);
    }
}
